function maxSubArray(nums: number[]): number {
    for (let i = 1; i < nums.length; i++){
        if(nums[i-1]>0){
            nums[i] = nums[i-1] + nums[i];
        }
    }
    return Math.max(...nums)
};

// 动态规划
// https://leetcode-cn.com/problems/maximum-subarray/solution/shi-pin-tu-jie-zui-da-zi-xu-he-by-alchemist-5r/
// 如果前一个最优解大于零则加上如果小于零则拷贝自己
// 在状态集中求最大值

const arr = [-2,1,-3,4,-1,2,1,-5,4];

console.log(maxSubArray(arr));